package com.taiyuan.tongcheng.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.ui.AnnotateUtil;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.utils.DensityUtils;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.taiyuan.tongcheng.application.AppConfig;
import com.taiyuan.tongcheng.jpush.MainActivity;
import com.taiyuan.tongcheng.map.LocationDemo;
import com.taiyuan.tongcheng.ui.MovingActivity;
import com.taiyuan.tongcheng.widget.DepthPageTransformer;
import com.taiyuan.tongcheng.widget.ViewPagerCompat;
import com.tytc56.user.R;

public class Fragment1 extends BaseFragment implements OnClickListener {

	private View contentView;

	@BindView(id = R.id.rl_moving, click = true)
	private RelativeLayout tcMovingBtn;
	@BindView(id = R.id.rl_taxi, click = true)
	private RelativeLayout tcTaxiBtn;
	@BindView(id = R.id.rl_tel, click = true)
	private RelativeLayout tcTelBtn;
	@BindView(id = R.id.id_viewpager)
	private ViewPagerCompat mViewPager;
	
	private int[] mImgIds = new int[] { R.drawable.home_banner,
			R.drawable.home_banner, R.drawable.home_banner };
	private List<ImageView> mImageViews = new ArrayList<ImageView>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 界面content
		contentView = inflater.inflate(R.layout.fragment_tab1, null);
		// 注解
		AnnotateUtil.initBindView(this, contentView);
		initView();
		return contentView;
	}

	@Override
	protected void initView() {
		super.initView();
		initButton();
		tcMovingBtn.setOnClickListener(this);
		initData();
		mViewPager.setPageTransformer(true, new DepthPageTransformer());
//		mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
		mViewPager.setAdapter(new PagerAdapter()
		{
			@Override
			public Object instantiateItem(ViewGroup container, int position)
			{
				container.addView(mImageViews.get(position));
				return mImageViews.get(position);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object)
			{

				container.removeView(mImageViews.get(position));
			}

			@Override
			public boolean isViewFromObject(View view, Object object)
			{
				return view == object;
			}

			@Override
			public int getCount()
			{
				return mImgIds.length;
			}
		});

	}

	
	private void initData()
	{
		for (int imgId : mImgIds)
		{
			ImageView imageView = new ImageView(getActivity());
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setImageResource(imgId);
			mImageViews.add(imageView);
		}
	}
	
	private void initButton() {
		// 输入框适配
		LinearLayout.LayoutParams inputParams = (LinearLayout.LayoutParams) tcTaxiBtn
				.getLayoutParams();
		
		inputParams.width = (int) (DensityUtils.getScreenW(getActivity()) * AppConfig.WidgetSize.main_item_width / 1080);
		inputParams.height = (int) (DensityUtils.getScreenW(getActivity()) * AppConfig.WidgetSize.main_item_width  / 1080 * AppConfig.WidgetSize.main_item_height /  AppConfig.WidgetSize.main_item_width);
		tcTaxiBtn.setLayoutParams(inputParams);
		tcMovingBtn.setLayoutParams(inputParams);
		tcTelBtn.setLayoutParams(inputParams);
		
		LinearLayout.LayoutParams banner = (LinearLayout.LayoutParams) mViewPager
				.getLayoutParams();
		
		banner.width = (int) (DensityUtils.getScreenW(getActivity()) * AppConfig.WidgetSize.main_banner_width / 1080);
		banner.height = (int) (DensityUtils.getScreenW(getActivity()) * AppConfig.WidgetSize.main_banner_width  / 1080 * AppConfig.WidgetSize.main_banner_height /  AppConfig.WidgetSize.main_banner_width);
		mViewPager.setLayoutParams(banner);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_moving:
			startActivity(new Intent(getActivity(), MovingActivity.class));
			break;
		case R.id.rl_taxi:
			startActivity(new Intent(getActivity(), LocationDemo.class));
			break;
		case R.id.rl_tel:
			startActivity(new Intent(getActivity(), MainActivity.class));
			break;

		default:
			break;
		}
	}
}
